package com.kgc.springboot.aviation.controller.gwd;

import com.aliyuncs.exceptions.ClientException;
import com.kgc.springboot.aviation.pojo.User;
import com.kgc.springboot.aviation.pojo.Users;
import com.kgc.springboot.aviation.service.gwd.LoginService;
import com.kgc.springboot.aviation.utils.SmsUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author : 郭未栋
 * @version : 1.0
 * @date ： Created in 2020/12/30
 * @description :
 */
@Controller
public class LoginAllController {
    @Resource
    LoginService loginService;
    //用户登录
    @PostMapping("/dologin")
    @ResponseBody
    public Map<String,Object> get(String userinp,String password, HttpSession session){
        Map<String,Object> map =new HashMap<>();
        Users users = loginService.login2(userinp, password);
        if (users!=null){
            //存储用户信息
            session.setAttribute("users",users);
            System.out.println("账号密码正确");
            map.put("code",0);
           return map;
        }else {
            System.out.println("账号密码错误");
            map.put("code",1);
            return map;
        }

    }

    @PostMapping("/checkPhone")
    @ResponseBody
    public Map<String,Object> getcode(String phone) throws ClientException {
        Users users = loginService.login3(phone);
        Map<String,Object> map =new HashMap<>();
        if (users!=null){
            System.out.println("进来短信验证了"+phone);

            map.put("code",0);
        }else {
            map.put("code",1);
        }
        return map;
    }
    @RequestMapping("getcode")
    @ResponseBody
    public Map<String,Object> getcode(HttpSession session,String phone) throws ClientException {
        Map<String,Object> map =new HashMap<>();

         int intCount = (new Random()).nextInt(999999);//
        if (intCount < 100000) intCount += 100000;
        String s = intCount + "";
        System.out.println(s);
        SmsUtils smsUtils=new SmsUtils();
                    smsUtils.sendSms(phone,s);
        session.setAttribute("yzm",s);
        System.out.println("进来短信验证了");
        return map;
    }
    @RequestMapping("plogin")
    @ResponseBody
    public Map<String,Object> plogin(String code,HttpSession session,String phone){
        Object yzm = session.getAttribute("yzm");
        System.out.println(yzm);
        Users users = loginService.login3(phone);
        Map<String,Object> map =new HashMap<>();
        session.setAttribute("users",users);
        if (code.equals(yzm)){
            map.put("code",0);
        }else {
            map.put("code",2);
        }
        return map;
    }
    @RequestMapping("reg")
    public String reg(){
        return "reg";
    }

    @RequestMapping("/rearlogin")
    public String rearlogin(String userName,String userPassword,HttpSession session,Model model){
        User user = loginService.login(userName, userPassword);
        System.out.println("用户名"+userName);
        System.out.println("密码"+userPassword);
        if (user!=null){
            session.setAttribute("user21",user);
            return "index2";
        }
//        session.invalidate();
//        HttpSession session = event.getSession()
        model.addAttribute("a","账号或密码错误请重新登录");
        return "登录";
    }
    @RequestMapping("selectUsers")
    public String selectUsers(String usersPhone, Model model){
        System.out.println(usersPhone);
        List<Users> select = loginService.select(usersPhone);
            model.addAttribute("select",select);
            System.out.println("电话正确进来了");
            System.out.println("有"+select.size()+"个用户");
            return "selectUsers2";
    }
    //用户注册
    @PostMapping("/checkPhone2")
    @ResponseBody
    public Map<String,Object> getcode2(String phone) throws ClientException {
        Users users = loginService.login3(phone);
        Map<String,Object> map =new HashMap<>();
        if (users!=null){
            System.out.println("进来短信验证了"+phone);

            map.put("code",1);
        }else {
            map.put("code",0);
        }
        return map;
    }

    @RequestMapping("addusers")
    public String addUsers( Users users,String yzm,HttpSession session,Model model){

        Object yzm1 = session.getAttribute("yzm");
        if (yzm.equals(yzm1)){
            Integer integer = loginService.addUsers(users);
            if (integer>0){
                System.out.println("添加成功");
                model.addAttribute("a","注册成功");
                return "index3";
            }else {
                System.out.println("            添加失败");
                model.addAttribute("a","注册失败");
                return "reg";
            }
        }else {
            model.addAttribute("a","验证码错误");
            return "reg";
        }

    }

    @RequestMapping("/getpass")//修改密码
    public String getpass(){
        return "getpass";
    }
    @RequestMapping("login1")
    public String login1(){
        return "index3";
    }

    @RequestMapping("upUsers")
    public String upusers(String phone,String yzm,String usersPassword,HttpSession session,Model model){
        Object yzm1 = session.getAttribute("yzm");
        Users users = loginService.login3(phone);
        if (users!=null){
        if (yzm==yzm1||yzm.equals(yzm1)){
            String usersName = users.getUsersName();
            Users users1=new Users();
            users1.setUsersPassword(usersPassword);
            users1.setUsersName(usersName);
            users1.setUsersPhone(phone);
            Integer integer = loginService.upUsers(users1);
            if (integer>0){
                model.addAttribute("a","修改成功");
                System.out.println("修改成功"+usersPassword);
                return "index3";
            }else {
                model.addAttribute("a","修改失败请确认手机号是否正确");
                return "getpass";

            }
        }else {
            model.addAttribute("a","验证码错误");
            return "getpass";
        }
        }else {
            model.addAttribute("a","修改失败请确认手机号是否正确");
            return "getpass";
        }
    }

    @RequestMapping("userinfo")//跳转个人信息
    public String ll(){
        return "权限用户管理界面";
    }

    @RequestMapping("userinfoupdate")//跳转修改密码信息
    public String lqwe(){
        return "权限用户修改";
    }
    @RequestMapping("updata2")//确认密码是否一至
    @ResponseBody
    public Map<String,Object> updata2(Integer id, String usersPassword, String newusersPassword, String confirmNewPassword, Model model){
        Map<String,Object> map =new HashMap<>();
        if (!newusersPassword.equals(confirmNewPassword)){
            map.put("aa",0);
            return map;
        }else {
            User user=new User();
            user.setId(id);
            System.out.println(id);
            User user1 = loginService.getUser(user);
            user1.setUserPassword(newusersPassword);
            Integer integer = loginService.upUser(user1);
            if (integer>0){
                map.put("aa",3);
            }
            map.put("aa",1);
        }
        return map;
    }
}
